<?php
namespace app\admin\controller;

use app\common\cache\common\IAreaCache;
use app\common\cache\user\IUserCache;
use app\common\cache\user\IUserLevelCache;
use app\common\controller\AdminBaseController;
use app\common\enum\TaskType;
use app\common\enum\UserPoolBidType;
use app\common\enum\PoolBidStatus;
use app\common\enum\UserStatus;
use app\common\ext\IDb;
use app\common\ext\IRequest;

class PoolBid extends AdminBaseController{
    /**
     * @功能：信贷公司意向客户池列表
     * @开发者：cxl
     * @return string
     */
    public function pool_bid_agent_list(){
        //接收参数
        $user_pool_bid_id = IRequest::get('user_pool_bid_id');
        $user_id = IRequest::get('user_id');
        $phone = IRequest::get('phone');
        $identity = IRequest::get('identity');
        $nick = IRequest::get('nick');
        $real = IRequest::get('real');
        $area = IRequest::get('area');
        $is_social = IRequest::get('is_social');
        $is_accumulation_fund= IRequest::get('is_accumulation_fund');
        $is_marry = IRequest::get('is_marry');
        $is_white = IRequest::get('is_white');
        $is_loan = IRequest::get('is_loan');
        $is_overdue = IRequest::get('is_overdue');
        $is_student = IRequest::get('is_student');
        $is_real = IRequest::get('is_real');
        $is_car = IRequest::get('is_car');
        $is_house = IRequest::get('is_house');
        $is_credit_card = IRequest::get('is_credit_card');
        $is_policy = IRequest::get('is_policy');
        $is_agent = IRequest::get('is_agent');
        $is_company = IRequest::get('is_company');

        //整理区域数据
        $province_id = 0;
        $city_id = 0;
        if(!empty($area) && is_array($area)){
            if(!empty($area[0])) $province_id = $area[0]; //取得省份
            if(!empty($area[1])) $city_id = $area[1]; //取得城市
        }

        $pool_bid_where['usp.user_pool_bid_type'] = UserPoolBidType::Intent;
        if(!empty($user_pool_bid_id)) $pool_bid_where['usp.user_pool_bid_id'] = $user_pool_bid_id;
        if(!empty($user_id)) $pool_bid_where['ur.user_id'] = $user_id;
        if(!empty($province_id)) $pool_bid_where['usp.province_id'] = $province_id;
        if(!empty($city_id)) $pool_bid_where['usp.city_id'] = $city_id;
        if(!empty($nick)) $pool_bid_where['ur.nick'] = array("like","%{$nick}%");
        if(!empty($phone)) $pool_bid_where['ur.phone'] = array("like","%{$phone}%");
        if(!empty($identity)) $pool_bid_where['ur.identity'] = $identity;
        if(!empty($real)) $pool_bid_where['ur.real'] = array("like","%{$real}%");
        if(!empty($is_social)) $pool_bid_where['ur.is_social'] = $is_social;
        if(!empty($is_accumulation_fund)) $pool_bid_where['ur.is_accumulation_fund'] = $is_accumulation_fund;
        if(!empty($is_marry)) $pool_bid_where['ur.is_marry'] = $is_marry;
        if(!empty($is_white)) $pool_bid_where['ur.is_white'] = $is_white;
        if(!empty($is_loan)) $pool_bid_where['ur.is_loan'] = $is_loan;
        if(!empty($is_overdue)) $pool_bid_where['ur.is_overdue'] = $is_overdue;
        if(!empty($is_student)) $pool_bid_where['ur.is_student'] = $is_student;
        if(!empty($is_real)) $pool_bid_where['ur.is_real'] = $is_real;
        if(!empty($is_car)) $pool_bid_where['ur.is_car'] = $is_car;
        if(!empty($is_house)) $pool_bid_where['ur.is_house'] = $is_house;
        if(!empty($is_credit_card)) $pool_bid_where['ur.is_credit_card'] = $is_credit_card;
        if(!empty($is_policy)) $pool_bid_where['ur.is_policy'] = $is_policy;
        if(!empty($is_company)) $pool_bid_where['ur.is_company'] = $is_company;
        $user_pool_bid_list = IDb::getInstance('user_pool_bid as usp')
            ->setDbFiled('usp.*,ur.user_id,ur.nick,ur.phone,ur.identity,ur.real,ur.avatar')
            ->setDbJoin('user as ur','ur.user_id=usp.user_id')
            ->setDbWhere($pool_bid_where)
            ->setDbOrder('usp.user_pool_bid_id desc')
            ->pag();
        if ($user_pool_bid_list === false){
            error('获取客户池失败');
        }
//        dump($user_pool_bid_list);
        //取得取得区域信息
        $area_list = IAreaCache::getAllList(2);

        $this->assign("user_pool_bid_list",$user_pool_bid_list);
        $this->assign("user_pool_bid_id",$user_pool_bid_id);
        $this->assign("area",$area);
        $this->assign("area_list",$area_list);
        $this->assign("user_id",$user_id);
        $this->assign("nick",$nick);
        $this->assign("phone",$phone);
        $this->assign("identity",$identity);
        $this->assign("real",$real);
        $this->assign("is_social",$is_social);
        $this->assign("is_accumulation_fund",$is_accumulation_fund);
        $this->assign("is_marry",$is_marry);
        $this->assign("is_white",$is_white);
        $this->assign("is_loan",$is_loan);
        $this->assign("is_overdue",$is_overdue);
        $this->assign("is_student",$is_student);
        $this->assign("is_real",$is_real);
        $this->assign("is_car",$is_car);
        $this->assign("is_house",$is_house);
        $this->assign("is_credit_card",$is_credit_card);
        $this->assign("is_policy",$is_policy);
        $this->assign("is_company",$is_company);
        $this->assign("is_agent",$is_agent);

        //渲染模板输出
        return view();
    }
    /**
     * @功能：信贷公司意向客户池详情
     * @开发者：cxl
     * @return string
     */
    public function pool_bid_agent_info(){
        //接收参数
        $user_pool_bid_id = IRequest::get("user_pool_bid_id",IRequest::NOT_EMPTY,"用户池编号不能为空！");

        $where['user_pool_bid_id'] = $user_pool_bid_id;
        $user_pool_bid_info = IDb::getInstance('user_pool_bid')->setDbWhere($where)->row();
        if ($user_pool_bid_info === false){
            error('获取意向详情失败');
        }

        //设置layout
        $this->layout("layout_empty");

        //页面赋值
        $this->assign("user_pool_bid_info",$user_pool_bid_info);

        //渲染模板输出
        return view();
    }
    /**
     * @功能：添加追踪记录
     * @开发者：wdd
     * @return string
     */
    public function user_pool_bid_task_add(){
        //接收参数
        $user_pool_bid_id = IRequest::get("user_pool_bid_id",IRequest::NOT_EMPTY,"用户池编号不能为空！");

        $pool_bid_where['usp.user_pool_bid_id'] = $user_pool_bid_id;
        $user_pool_bid_info = IDb::getInstance('user_pool_bid as usp')
            ->setDbFiled('usp.*,ur.user_id,ur.nick,ur.phone,ur.identity,ur.real,ur.avatar')
            ->setDbJoin('user as ur','ur.user_id=usp.user_id')
            ->setDbWhere($pool_bid_where)
            ->row();
        if ($user_pool_bid_info === false){
            error('获取客户池失败');
        }

        //页面赋值
        $this->assign("user_pool_bid_id",$user_pool_bid_id);
        $this->assign("user_pool_bid_info",$user_pool_bid_info);

        return view();
    }

    /**
     * @功能：添加追踪记录提交
     * @开发者：wdd
     * @return string
     */
    public function user_pool_bid_task_submit(){
        //接收参数
        $user_pool_bid_id = IRequest::get("user_pool_bid_id",IRequest::NOT_EMPTY,"用户池编号不能为空！");
        $operate_note = IRequest::get('operate_note',IRequest::NOT_EMPTY,"内容不能为空！");

        $where['user_pool_bid_id'] = $user_pool_bid_id;
        $user_pool_bid = IDb::getInstance('user_pool_bid')->setDbWhere($where)->row();
        if($user_pool_bid === false){
            error('获取客户池详情失败');
        }
        if (empty($user_pool_bid['user_id'])){
            error('不是意向客户池，不能添加记录');
        }

        $data['user_pool_bid_id'] = $user_pool_bid_id;
        $data['user_id'] = $user_pool_bid['user_id'];
        $data['task_type'] = TaskType::tail;
        $data['operate_note'] = $operate_note;
        $data['admin_id'] = get_login_admin_id();
        $data['operate_time'] = get_date_time();
        $user_pool_bid_task_add = IDb::getInstance('user_pool_bid_task')->setDbData($data)->add();
        if ($user_pool_bid_task_add === false){
            error('添加追踪记录失败');
        }

        //画面跳转
        $this->success("操作成功！", Url("pool_bid_agent_list"));
    }

    /**
     * @功能：追踪记录日志
     * @开发者：wdd
     * @return string
     */
    public function user_pool_bid_task_list(){
        //接收参数
        $user_pool_bid_id = IRequest::get("user_pool_bid_id",IRequest::NOT_EMPTY,"用户池编号不能为空！");

        $pool_bid_where['usp.user_pool_bid_id'] = $user_pool_bid_id;
        $user_pool_bid_info = IDb::getInstance('user_pool_bid as usp')
            ->setDbFiled('usp.*,ur.user_id,ur.nick,ur.phone,ur.identity,ur.real,ur.avatar')
            ->setDbJoin('user as ur','ur.user_id=usp.user_id')
            ->setDbWhere($pool_bid_where)
            ->row();
        if ($user_pool_bid_info === false){
            error('获取客户池失败');
        }

        $where['up.user_pool_bid_id'] = $user_pool_bid_id;
        $where['up.task_type'] = TaskType::tail;
        $user_pool_bid_task_list = IDb::getInstance('user_pool_bid_task as up')
            ->setDbFiled('ur.*,up.*')
            ->setDbJoin('user as ur','ur.user_id=up.user_id')
            ->setDbWhere($where)
            ->setDbOrder('up.user_pool_bid_task_id desc')
            ->pag();
        if ($user_pool_bid_task_list === false){
            error('获取追踪记录失败');
        }

        //设置layout
        $this->layout("layout_empty");

        $this->assign("user_pool_bid_task_list",$user_pool_bid_task_list);
        $this->assign("user_pool_bid_info",$user_pool_bid_info);

        //渲染模板输出
        return view();

    }

    /**
     * @功能：合作商用户池列表
     * @param $view string 模板名称
     * @开发者：cxl
     * @return string
     */
    public function pool_bid_not_list(){
        //接收参数
        $user_pool_bid_id = IRequest::get('user_pool_bid_id');
        $cooperate_admin_id = IRequest::get('cooperate_admin_id');
        $cooperate_id = IRequest::get('cooperate_id');
        $user_id = IRequest::get('user_id');
        $phone = IRequest::get('phone');
        $user_phone = IRequest::get('user_phone');
        $identity = IRequest::get('identity');
        $area = IRequest::get('area');
        $pool_bid_status = IRequest::get('pool_bid_status');

        //整理区域数据
        $province_id = 0;
        $city_id = 0;
        if(!empty($area) && is_array($area)){
            if(!empty($area[0])) $province_id = $area[0]; //取得省份
            if(!empty($area[1])) $city_id = $area[1]; //取得城市
        }

        $pool_bid_where['usp.user_pool_bid_type'] = UserPoolBidType::Cooperate;
        if(!empty($user_pool_bid_id)) $pool_bid_where['usp.user_pool_bid_id'] = $user_pool_bid_id;
        if(!empty($cooperate_admin_id)) $pool_bid_where['usp.cooperate_admin_id'] = $cooperate_admin_id;
        if(!empty($cooperate_id)) $pool_bid_where['usp.cooperate_id'] = $cooperate_id;
        if(!empty($province_id)) $pool_bid_where['usp.province_id'] = $province_id;
        if(!empty($city_id)) $pool_bid_where['usp.city_id'] = $city_id;
        if(!empty($user_id)) $pool_bid_where['usp.user_id'] = $user_id;
        if(!empty($phone)) $pool_bid_where['ca.phone'] = array("like","%{$phone}%");
        if(!empty($user_phone)) $pool_bid_where['us.phone'] = array("like","%{$user_phone}%");
        if(!empty($identity)) $pool_bid_where['ca.identity'] = $identity;
        if(!empty($pool_bid_status)) $pool_bid_where['usp.pool_bid_status'] = $pool_bid_status;

        $user_pool_bid_list = IDb::getInstance('user_pool_bid as usp')
            ->setDbFiled('usp.*,ca.phone as c_phone,us.phone as u_phone')
            ->setDbJoin('cooperate_admin as ca','ca.cooperate_admin_id=usp.cooperate_admin_id')
            ->setDbJoin('user as us','us.user_id=usp.user_id')
            ->setDbWhere($pool_bid_where)
            ->setDbOrder('usp.user_pool_bid_id desc')
            ->pag();
        if ($user_pool_bid_list === false){
            error('获取客户池失败');
        }

        //取得取得区域信息
        $area_list = IAreaCache::getAllList(2);

        $this->assign("user_pool_bid_list",$user_pool_bid_list);
        $this->assign("user_pool_bid_id",$user_pool_bid_id);
        $this->assign("cooperate_id",$cooperate_id);
        $this->assign("user_id",$user_id);
        $this->assign("area",$area);
        $this->assign("area_list",$area_list);
        $this->assign("cooperate_admin_id",$cooperate_admin_id);
        $this->assign("phone",$phone);
        $this->assign("user_phone",$user_phone);
        $this->assign("identity",$identity);
        $this->assign("pool_bid_status",$pool_bid_status);

        //渲染模板输出
        return view();
    }

    /**
     * @功能：合作商意向客户池详情
     * @开发者：cxl
     * @return string
     */
    public function pool_bid_cooperate_info(){
        //接收参数
        $user_pool_bid_id = IRequest::get("user_pool_bid_id",IRequest::NOT_EMPTY,"用户池编号不能为空！");

        $where['user_pool_bid_id'] = $user_pool_bid_id;
        $user_pool_bid_info = IDb::getInstance('user_pool_bid')->setDbWhere($where)->row();
        if ($user_pool_bid_info === false){
            error('获取意向详情失败');
        }

        //设置layout
        $this->layout("layout_empty");

        //页面赋值
        $this->assign("user_pool_bid_info",$user_pool_bid_info);

        //渲染模板输出
        return view();
    }

    /**
     * @功能：投标记录
     * @开发者：cxl
     * @return string
     */
    public function bid_tender_log(){
        //接收参数
        $user_pool_bid_id = IRequest::get("user_pool_bid_id",IRequest::NOT_EMPTY,"客户池编号不能为空！");
        $agent_id = IRequest::get("agent_id");
        $agent_name = IRequest::get("agent_name");

        //判断客户池类型
        $agent_admin_tender_where['user_pool_bid_id'] = $user_pool_bid_id;
        if(!empty($agent_id))$agent_admin_tender_where['agent_id'] = $agent_id;
        if(!empty($agent_name))$agent_admin_tender_where['agent_name'] = array("like","%{$agent_name}%");
        $agent_admin_tender_list = IDb::getInstance("agent_admin_tender")->setDbWhere($agent_admin_tender_where)->pag();
        if ($agent_admin_tender_list === false){
            error('获取投标记录失败');
        }

        //设置layout
        $this->layout("layout_empty");

        //页面赋值
        $this->assign("agent_id",$agent_id);
        $this->assign("user_pool_bid_id",$user_pool_bid_id);
        $this->assign("agent_name",$agent_name);
        $this->assign("agent_admin_tender_list",$agent_admin_tender_list);

        //渲染模板输出
        return view();
    }
    /**
     * @功能：合作商客户池追踪记录日志
     * @开发者：wdd
     * @return string
     */
    public function pool_bid_not_task_list(){
        //接收参数
        $user_pool_bid_id = IRequest::get("user_pool_bid_id",IRequest::NOT_EMPTY,"用户池编号不能为空！");

        $pool_bid_where['usp.user_pool_bid_id'] = $user_pool_bid_id;
        $user_pool_bid_info = IDb::getInstance('user_pool_bid as usp')
            ->setDbFiled('usp.*,ur.user_id,ur.nick,ur.phone,ur.identity,ur.real,ur.avatar')
            ->setDbJoin('user as ur','ur.user_id=usp.user_id')
            ->setDbWhere($pool_bid_where)
            ->row();
        if ($user_pool_bid_info === false){
            error('获取客户池失败');
        }

        $where['up.user_pool_bid_id'] = $user_pool_bid_id;
        $user_pool_bid_task_list = IDb::getInstance('user_pool_bid_task as up')
            ->setDbFiled('up.*,ca.*')
            ->setDbJoin('user_pool_bid as ub','ub.user_pool_bid_id=up.user_pool_bid_id')
            ->setDbJoin('cooperate_admin as ca','ca.cooperate_admin_id=ub.cooperate_admin_id')
            ->setDbWhere($where)
            ->setDbOrder('up.user_pool_bid_task_id desc')
            ->pag();
        if ($user_pool_bid_task_list === false){
            error('获取追踪记录失败');
        }

        //设置layout
        $this->layout("layout_empty");

        $this->assign("user_pool_bid_task_list",$user_pool_bid_task_list);
        $this->assign("user_pool_bid_info",$user_pool_bid_info);

        //渲染模板输出
        return view();

    }

    /**
     * @功能：图片列表
     * @开发者：wdd
     * @return string
     */
    public function img_list(){
        //接收参数
        $user_pool_bid_task_id = IRequest::get("user_pool_bid_task_id",IRequest::NOT_EMPTY,"操作编号不能为空！");

        $where['user_pool_bid_task_id'] =  $user_pool_bid_task_id;
        $img_list = IDb::getInstance('user_pool_bid_task')->setDbFiled('img_list')->setDbWhere($where)->row();
        if ($img_list === false){
            error('获取图片列表失败');
        }
        $img_list = explode("{|}", $img_list['img_list']);
        //设置layout
        $this->layout("layout_empty");

        $this->assign("img_list",$img_list);
        $this->assign("user_pool_bid_task_id",$user_pool_bid_task_id);

        //渲染模板输出
        return view();
    }

    /**
     * @功能：图片列表
     * @开发者：wdd
     * @return string
     */
    public function video_list(){
        //接收参数
        $user_pool_bid_task_id = IRequest::get("user_pool_bid_task_id",IRequest::NOT_EMPTY,"操作编号不能为空！");

        $where['user_pool_bid_task_id'] =  $user_pool_bid_task_id;
        $video_list = IDb::getInstance('user_pool_bid_task')->setDbFiled('video_list')->setDbWhere($where)->row();
        if ($video_list === false){
            error('获取视频列表失败');
        }
        $video_list = explode("{|}", $video_list['video_list']);
        //设置layout
        $this->layout("layout_empty");

        $this->assign("video_list",$video_list);
        $this->assign("user_pool_bid_task_id",$user_pool_bid_task_id);

        //渲染模板输出
        return view();
    }
    /**
     * @功能：语音列表
     * @开发者：wdd
     * @return string
     */
    public function audio_list(){
        //接收参数
        $user_pool_bid_task_id = IRequest::get("user_pool_bid_task_id",IRequest::NOT_EMPTY,"操作编号不能为空！");

        $where['user_pool_bid_task_id'] =  $user_pool_bid_task_id;
        $audio_list = IDb::getInstance('user_pool_bid_task')->setDbFiled('audio_list')->setDbWhere($where)->row();
        if ($audio_list === false){
            error('获取语音列表失败');
        }
        $audio_list = explode("{|}", $audio_list['audio_list']);
        //设置layout
        $this->layout("layout_empty");

        $this->assign("audio_list",$audio_list);
        $this->assign("user_pool_bid_task_id",$user_pool_bid_task_id);

        //渲染模板输出
        return view();
    }
}